package com.slz3.business.module.instantMessaging.insMessaging.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.instantMessaging.insMessaging.domain.InsMessaging;
import com.slz3.business.module.instantMessaging.insMessaging.domain.InsMessagingFactory;
import com.slz3.business.module.instantMessaging.insMessaging.steward.InsMessagingSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class InsMessagingServiceImpl implements InsMessagingService {
    @Override
    public InsMessaging save(InsMessaging insMessaging) {
        return Repositorys.insMessaging.save(InsMessagingFactory.initBaseFields(insMessaging));
    }

    @Override
    public Page<InsMessaging> page(MultiValueMap<String, String> params) {
        Page<InsMessaging> insMessagingPage = Repositorys.insMessaging.findAll(
                InsMessagingSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return insMessagingPage;
    }


    @Override
    public InsMessaging modify(InsMessaging insMessagingNew) {
        return Repositorys.insMessaging.saveAndFlush(InsMessagingFactory.initBaseFields(insMessagingNew));
    }

    @Override
    public Optional<InsMessaging> single(UUID uuid) {
        return Repositorys.insMessaging.findById(uuid);
    }

    @Override
    public List<InsMessaging> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.insMessaging.findAllById(uuids);
    }

    @Override
    public List<InsMessaging> delete(List<InsMessaging> insMessagingList) {
        insMessagingList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            InsMessagingFactory.initBaseFields(item);
        });
        return Services.insMessagingTransactional.batchModify(insMessagingList);
    }

}
